﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebForm_ManageUser_ucEditUser : System.Web.UI.UserControl
{
    protected virtual void Page_Init(object sender, System.EventArgs e)
    {
        FormView1.InsertItemTemplate = FormView1.ItemTemplate;
        FormView1.EditItemTemplate = FormView1.ItemTemplate;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    //所屬單位加上"請選擇"
    protected void ddlOrganizationID_DataBound(object sender, EventArgs e)
    {
        ((DropDownList)sender).Items.Insert(0, new ListItem("請選擇", "0"));
    }


    public delegate void GoSavedDelegate(object sender, EventArgs e);
    public event GoSavedDelegate GoSaved;

    protected void btnSave_Click(object sender, EventArgs e)
    {

        try
        {
            if (DataAccessMode == FormViewMode.Insert)
            {
                FormView1.InsertItem(true);
            }
            else if (DataAccessMode == FormViewMode.Edit)
            {

                FormView1.UpdateItem(true);
            }

            this.SaveProperty();

            if (GoSaved != null)
            {
                GoSaved(this, e);
            }
        }
        catch (Exception ex)
        {

            if (((System.Data.SqlClient.SqlException)ex.InnerException).Number == 2627)
            {
                CommonLib2.clsCommon.ShowMessage(true, CommonLib2.clsCommon.RegisterType.RegisterStartupScript, "人員名稱已存在!請使用別的名稱。");
            }
            else
            {
                CommonLib2.clsCommon.ShowMessage(true, CommonLib2.clsCommon.RegisterType.RegisterStartupScript, ex.Message.Replace("'", "\\'"));
            }
        }



    }
    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        e.Values["OrganizationID"] = ((DropDownList)FormView1.FindControl("ddlOrganizationID")).SelectedValue;
        e.Values["Active"] = ((CheckBox)FormView1.FindControl("chkActive")).Checked == true ? "Y" : "N";

    }
    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        e.NewValues["OrganizationID"] = ((DropDownList)FormView1.FindControl("ddlOrganizationID")).SelectedValue;
        e.NewValues["Active"] = ((CheckBox)FormView1.FindControl("chkActive")).Checked == true ? "Y" : "N";
    }
    protected void ddlOrganizationID_DataBinding(object sender, EventArgs e)
    {
        //如果是環保局人員(2),則 MyOrganizationID 改成台北縣政府(15)
        ObjectDataSource ObjectDataSource_Organization = (ObjectDataSource)FormView1.FindControl("ObjectDataSource_Organization");
        ObjectDataSource_Organization.SelectParameters["MyOrganizationID"].DefaultValue = (CS_clsAuth.MyOrganizationID == "2" ? "15" : CS_clsAuth.MyOrganizationID);

    }

    protected void odsProperty_OnPreSelecting(object sender, EventArgs e)
    {
        if (this.FormView1.FindTxtControl("txtEmployeeID").Text != "")
        {
            this.FormView1.FindTableDataSourceControl("odsProperty").SelectParameters["EmployeeID"].DefaultValue = this.FormView1.FindTxtControl("txtEmployeeID").Text;
        }
        else
        {
            this.FormView1.FindTableDataSourceControl("odsProperty").SelectParameters["EmployeeID"].DefaultValue = "_"; // 用於新增時
        }
    }

}



public partial class WebForm_ManageUser_ucEditUser
{

    public void InitData(string strId)
    {

        EmployeeId = strId;

        if (strId == "0")
        {
            //新增
            lblTitle.Text = this.GetLocalResourceObject("TitleAdd").ToString();
            this.DataAccessMode = FormViewMode.Insert;

            this.FormView1.ChangeMode(FormViewMode.Insert);
            ((CheckBox)FormView1.FindControl("chkActive")).Checked = true;

        }
        else
        {
            //編輯
            lblTitle.Text = this.GetLocalResourceObject("TitleEdit").ToString();
            this.DataAccessMode = FormViewMode.Edit;
            this.FormView1.ChangeMode(FormViewMode.Edit);

            ObjectDataSource1.SelectParameters["EmployeeId"].DefaultValue = EmployeeId;
            ObjectDataSource1.SelectParameters["OrganizationId"].DefaultValue = "0";
            ObjectDataSource1.SelectParameters["MyOrganizationId"].DefaultValue = (CS_clsAuth.MyOrganizationID == "2" ? "15" : CS_clsAuth.MyOrganizationID);

            FormView1.DataSourceID = ObjectDataSource1.ID;
            FormView1.DataBind();

            ((TextBox)FormView1.FindControl("txtEmployeeID")).Enabled = false;

        }

        //MyObj.WriteLog(this.FormView1.FindTableDataSourceControl("odsProperty").BuildSelectQuery());
    }

    public bool SaveProperty()
    {
        clsData dat = new clsData();

        using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
        {
            MyDAO.DeleteStringBuilder delSql = new MyDAO.DeleteStringBuilder("FD_EmployeeProperty");

            delSql.AddCondition("EmployeeID", this.FormView1.FindTxtControl("txtEmployeeID").Text);

            dat.AccData(delSql);

            foreach (My.MyGridViewRow row in this.FormView1.FindGVControl("gvProperty").Rows)
            {
                MyDAO.InsertStringBuilder sql = new MyDAO.InsertStringBuilder("FD_EmployeeProperty");

                sql.AddAssignment("EmployeeID", this.FormView1.FindTxtControl("txtEmployeeID").Text);
                sql.AddAssignment("PropertyName", this.FormView1.FindGVControl("gvProperty").Data[row.RowIndex]["PropertyName"]);
                sql.AddAssignment("PropertyValue", row.FindTxtControl("txtPropertyValue").Text);

                dat.AccData(sql);
            }

            scope.Complete();
        }

        return true;

    }

    public FormViewMode DataAccessMode
    {
        get
        {
            if (ViewState["DataAccessMode"] == null)
            {
                return FormViewMode.Insert;
            }

            return (FormViewMode)ViewState["DataAccessMode"];
        }
        set { ViewState["DataAccessMode"] = value; }
    }


    public string EmployeeId
    {
        get { return ViewState["EmployeeId"].ToString(); }
        set { ViewState["EmployeeId"] = value; }
    }


    protected object BooleanConverter(object strTMP)
    {
        if (strTMP == null)
        {
            return false;
        }
        else
        {
            return (strTMP.ToString() == "Y" ? true : false);
        }




    }



}




